﻿using System;
using System.Collections.Generic;
using System.Helpers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataProcessModule.Protocol
{
    public class MotorBatteryAlarmStats : BaseInstruction
    {

        #region param

        private MotorBatteryState MotorBatteryStat;//动力蓄电池报警标志	2	WORD

        private int IssueMotorBatteriesNumber;//动力蓄电池故障总数n	1	BYTE

        private int IssuePowerSupplyNumber;//电机故障总数N	1	BYTE

        private int IssueNumber;//故障总数M	1	BYTE

        #endregion

        #region attributes

        public MotorBatteryState AtMotorBatteryStat { get { return MotorBatteryStat; } }

        public int AtIssueMotorBatteriesNumber { get { return IssueMotorBatteriesNumber; } }

        public int AtIssuePowerSupplyNumber { get { return IssuePowerSupplyNumber; } }

        public int AtIssueNumber { get { return IssueNumber; } }

        #endregion

        #region construction

        public MotorBatteryAlarmStats()
        {
            instHeadInfo.instID = 0x0115;
        }

        public MotorBatteryAlarmStats(byte[] buf)
        {
            MotorBatteryStat = new MotorBatteryState();
            int offset = 0;
            // 获取头信息
            GetHeadInfo(buf, ref offset);
            BodyStreamSplit(buf, ref offset);
        }

        #endregion

        #region function

        public void BodyStreamSplit(byte[] buf, ref int offset)
        {
            try
            {
                int AlarmFlag = BC.ToUInt16Net(buf, ref offset);
                IssueMotorBatteriesNumber = buf[offset++];
                offset++;
                IssuePowerSupplyNumber = buf[offset++];
                offset++;
                IssueNumber = buf[offset++];
                MotorBatteryStat.TempratureAlarm = AlarmFlag & 1;
                MotorBatteryStat.BatteryOverHeatAlarm = AlarmFlag & 2;
                MotorBatteryStat.MotorBatteryPackVoltageOverloadedAlarm = AlarmFlag & 4;
                MotorBatteryStat.BatteryPackVoltageInsufficientAlarm = AlarmFlag & 8;
                MotorBatteryStat.SocRuningLowAlarm = AlarmFlag & 16;
                MotorBatteryStat.SingleBatteryPackVoltageOverloadedAlarm = AlarmFlag & 32;
                MotorBatteryStat.SingleBatteryPackVoltageInsufficientAlarm = AlarmFlag & 64;
                MotorBatteryStat.SocRuningOutAlarm = AlarmFlag & 128;
                MotorBatteryStat.SocOverloadedAlarm = AlarmFlag & 256;
                MotorBatteryStat.MotorBatteryPackMismatchAlarm = AlarmFlag & 512;
                MotorBatteryStat.MotorBatteriesCoopUnwellAlram = AlarmFlag & 1024;
            }
            catch (Exception e) { }
        }

        public string CombinedSqlString()
        {
            string _sql = string.Empty;
            _sql = "INSERT INTO table_power_alarm_data (TerminalKey,IssueMotorBatteriesNumber" +
               ",IssuePowerSupplyNumber,IssueNumber,TempratureAlarm,BatteryOverHeatAlarm" +
               ",MotorBatteryPackVoltageOverloadedAlarm,BatteryPackVoltageInsufficientAlarm" +
               ",SocRuningLowAlarm,SingleBatteryPackVoltageOverloadedAlarm,SingleBatteryPackVoltageInsufficientAlarm" +
               ",SocRuningOutAlarm,SocOverloadedAlarm,MotorBatteryPackMismatchAlarm" +
               ",MotorBatteriesCoopUnwellAlram) VALUES('" + instHeadInfo.tdPhoneNumber + "'," +
                IssueMotorBatteriesNumber + ',' + IssuePowerSupplyNumber + ',' + IssueNumber + ',' +
                MotorBatteryStat.TempratureAlarm + ',' + 
                MotorBatteryStat.BatteryOverHeatAlarm + ',' +               
                MotorBatteryStat.MotorBatteryPackVoltageOverloadedAlarm + ',' +
                MotorBatteryStat.BatteryPackVoltageInsufficientAlarm + ',' +
                MotorBatteryStat.SocRuningLowAlarm + ',' +
                MotorBatteryStat.SingleBatteryPackVoltageOverloadedAlarm + ',' +
                MotorBatteryStat.SingleBatteryPackVoltageInsufficientAlarm + ',' +
                MotorBatteryStat.SocRuningOutAlarm + ',' + 
                MotorBatteryStat.SocOverloadedAlarm + ',' +
                MotorBatteryStat.MotorBatteryPackMismatchAlarm + ',' + 
                MotorBatteryStat.MotorBatteriesCoopUnwellAlram + ")" +
                "ON DUPLICATE KEY UPDATE IssueMotorBatteriesNumber=VALUES(IssueMotorBatteriesNumber)," +
                "IssuePowerSupplyNumber=VALUES(IssuePowerSupplyNumber),IssueNumber=VALUES(IssueNumber)," +
                "TempratureAlarm=VALUES(TempratureAlarm),BatteryOverHeatAlarm=VALUES(BatteryOverHeatAlarm)" +
                ",MotorBatteryPackVoltageOverloadedAlarm=VALUES(MotorBatteryPackVoltageOverloadedAlarm)" +
                ",BatteryPackVoltageInsufficientAlarm=VALUES(BatteryPackVoltageInsufficientAlarm)" +
                ",SocRuningLowAlarm=VALUES(SocRuningLowAlarm),SingleBatteryPackVoltageOverloadedAlarm=VALUES(SingleBatteryPackVoltageOverloadedAlarm)" +
                ",SingleBatteryPackVoltageInsufficientAlarm=VALUES(SingleBatteryPackVoltageInsufficientAlarm)" +
                ",SocRuningOutAlarm=VALUES(SocRuningOutAlarm),SocOverloadedAlarm=VALUES(SocOverloadedAlarm)" +
                ",MotorBatteryPackMismatchAlarm=VALUES(MotorBatteryPackMismatchAlarm)" +
                ",MotorBatteriesCoopUnwellAlram=VALUES(MotorBatteriesCoopUnwellAlram);";
            return _sql;
        }
        public string InsertString()
        {
            string _sql = string.Empty;
            _sql = "INSERT INTO table_power_alarm_data_" + DateTime.Now.ToString("yyyyMMdd") + " (TerminalKey,IssueMotorBatteriesNumber" +
               ",IssuePowerSupplyNumber,IssueNumber,TempratureAlarm,BatteryOverHeatAlarm" +
               ",MotorBatteryPackVoltageOverloadedAlarm,BatteryPackVoltageInsufficientAlarm" +
               ",SocRuningLowAlarm,SingleBatteryPackVoltageOverloadedAlarm,SingleBatteryPackVoltageInsufficientAlarm" +
               ",SocRuningOutAlarm,SocOverloadedAlarm,MotorBatteryPackMismatchAlarm" +
               ",MotorBatteriesCoopUnwellAlram) VALUES('" + instHeadInfo.tdPhoneNumber + "'," +
                IssueMotorBatteriesNumber + ',' + IssuePowerSupplyNumber + ',' + IssueNumber + ',' +
                MotorBatteryStat.TempratureAlarm + ',' +
                MotorBatteryStat.BatteryOverHeatAlarm + ',' +
                MotorBatteryStat.MotorBatteryPackVoltageOverloadedAlarm + ',' +
                MotorBatteryStat.BatteryPackVoltageInsufficientAlarm + ',' +
                MotorBatteryStat.SocRuningLowAlarm + ',' +
                MotorBatteryStat.SingleBatteryPackVoltageOverloadedAlarm + ',' +
                MotorBatteryStat.SingleBatteryPackVoltageInsufficientAlarm + ',' +
                MotorBatteryStat.SocRuningOutAlarm + ',' +
                MotorBatteryStat.SocOverloadedAlarm + ',' +
                MotorBatteryStat.MotorBatteryPackMismatchAlarm + ',' +
                MotorBatteryStat.MotorBatteriesCoopUnwellAlram + ");";
            return _sql;
        }
        #endregion
    }

    #region struct

    public struct MotorBatteryState
    {
        public int TempratureAlarm;//bit0 1：温度差异报警；0：正常

        public int BatteryOverHeatAlarm;//bit1 1：电池极柱高温报警；0：正常

        public int MotorBatteryPackVoltageOverloadedAlarm;//bit2 1：动力蓄电池包过压报警；0：正常

        public int BatteryPackVoltageInsufficientAlarm;//bit3 1：电力蓄电池包欠压报警；0：正常

        public int SocRuningLowAlarm;//bit4 1：SOC低报警；0：正常

        public int SingleBatteryPackVoltageOverloadedAlarm;//bit5 1：单体蓄电池过压报警；0：正常

        public int SingleBatteryPackVoltageInsufficientAlarm;//bit6 1：单体蓄电池欠压报警；0：正常

        public int SocRuningOutAlarm;//bit7 1：SOC太低报警；0：正常

        public int SocOverloadedAlarm;//bit8 1：SOC过高报警；0：正常

        public int MotorBatteryPackMismatchAlarm;//bit9 1：动力蓄电池包不匹配报警；0：正常

        public int MotorBatteriesCoopUnwellAlram;//bit10 1：动力蓄电池一致性差报警；0：正常
    }

    #endregion
}

